﻿/*	
	stencil - action script 3 isometric game engine.

	http://code.google.com/p/stencilas3/
	
	Copyright (c) 2010 Max Zhuravkov, All Rights Reserved.

	Permission is hereby granted, free of charge, to any person obtaining a copy of
	this software and associated documentation files (the "Software"), to deal in
	the Software without restriction, including without limitation the rights to
	use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
	of the Software, and to permit persons to whom the Software is furnished to do
	so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all
	copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	SOFTWARE.
*/
package stencil.core.engine.display {
	
	import flash.geom.Rectangle;
	import stencil.core.engine.IComponentList;
	import stencil.core.engine.ComponentList;
	import stencil.core.engine.IComponent;
	import stencil.core.engine.ISceneObject;
	import stencil.core.engine.events.ComponentEvent;
	
	/**
	 *
	 * @author Max Zhuravkov
	 */
	public class CompositeSpriteObject extends SpriteObject implements IComponentList {
		
		private var _componentList:IComponentList;
		
		//------------------------------------------------------------------------------------------------
		// Constructor
		//------------------------------------------------------------------------------------------------
		
		public function CompositeSpriteObject() {
			this._componentList = new ComponentList();
			this._componentList.addEventListener(ComponentEvent.STN_ADD_COMPONENT, this.addComponentHandler);
			this._componentList.addEventListener(ComponentEvent.STN_REMOVE_COMPONENT, this.removeComponentHandler);
		}
		
		
		//------------------------------------------------------------------------------------------------
		// Public properties
		//------------------------------------------------------------------------------------------------
		
		/**
		 * Serialization support.
		 * @private
		 */
		public final function set components(value:Array):void {
			this._componentList.components = value;
		}
		
		/**
		 * @inheritDoc
		 */
		public final function get components():Array {
			return this._componentList.components;
		}
		
		//------------------------------------------------------------------------------------------------
		// Public methods
		//------------------------------------------------------------------------------------------------
		
		/**
		 * @inheritDoc
		 */
		public final function addComponent(id:String, component:IComponent, data:Object = null):IComponent {
			this.checkState();
			return this.componentList.addComponent(id, component, data);
		}
		
		/**
		 * @inheritDoc
		 */
		public final function addComponentType(id:String,componentType:Class,data:Object = null):IComponent {
			this.checkState();
			return this.componentList.addComponentType(id, componentType, data);
		}
		
		
		/**
		 * @inheritDoc
		 */
		public final function containsComponent(component:IComponent):Boolean {
			this.checkState();
			return this.componentList.containsComponent(component);
		}
		
		
		/**
		 * @inheritDoc
		 */
		public final function containsComponentType(componentType:Class):Boolean {
			this.checkState();
			return this.componentList.containsComponentType(componentType);
		}
		
		
		/**
		 * @inheritDoc
		 */
		public final function getComponentByType(componentType:Class):IComponent {
			this.checkState();
			return this.componentList.getComponentByType(componentType);
		}
		
		/**
		 * @inheritDoc
		 */
		public final function getComponentsByType(componentType:Class):Array {
			this.checkState();
			return this.componentList.getComponentsByType(componentType);
		}
		
		/**
		 * @inheritDoc
		 */
		public final function getComponentById(id:String):IComponent {
			this.checkState();
			return this.componentList.getComponentById(id);
		}
		
		/**
		 * @inheritDoc
		 */
		public final function removeComponent(component:IComponent):IComponent {
			this.checkState();
			return this.componentList.removeComponent(component);
		}
		
		/**
		 * @inheritDoc
		 */
		public final function removeComponentsByType(componentType:Class):Array {
			this.checkState();
			return this.componentList.removeComponentsByType(componentType);
		}
		
		/**
		 * @inheritDoc
		 */
		public final function removeComponentById(id:String):IComponent {
			this.checkState();
			return this.componentList.removeComponentById(id);
		}
		
		/**
		 * @inheritDoc
		 */
		public final function removeAllComponents():Array {
			this.checkState();
			
			return this.componentList.removeAllComponents();
		}
		
		/**
		 * @inheritDoc
		 */
		override public function render(viewPort:Rectangle, time:Number):void {
			super.render(viewPort, time);
			this.updateComponents(viewPort, time);
		}
		
		//------------------------------------------------------------------------------------------------
		// Protected properties
		//------------------------------------------------------------------------------------------------
		
		protected final function get componentList():IComponentList {
			return this._componentList;
		}
		
		//------------------------------------------------------------------------------------------------
		// Protected methods
		//------------------------------------------------------------------------------------------------
		
		
		/**
		 * @inheritDoc
		 */
		override protected function ownerAttached(owner:ISceneObject):void {
			super.ownerAttached(owner);
			
			this.componentList.owner = owner;
		}
		
		/**
		 * @inheritDoc
		 */
		override protected function ownerDetached(owner:ISceneObject):void {
			super.ownerDetached(owner);
			
			
		}
		
		protected function updateComponents(viewPort:Rectangle, time:Number):void {
			for each(var component:IComponent in this.components) {
				this.updateComponent(component, viewPort, time);
			}
		}
		
		protected function updateComponent(component:IComponent, viewPort:Rectangle, time:Number):void {
			
			if (component is IGraphicComponent) {
				var g:IGraphicComponent = component as IGraphicComponent;
				
				if (component.enabled) {
					g.render(viewPort, time);
				}
				
			}
		}
		
		protected function addComponentHandler(event:ComponentEvent):void {
			if (event.component is IGraphicComponent) {
				var g:IGraphicComponent = event.component as IGraphicComponent;
				
				this.addChild(g.content);
			}
		}
		
		protected function removeComponentHandler(event:ComponentEvent):void {
			if (event.component is IGraphicComponent) {
				var g:IGraphicComponent = event.component as IGraphicComponent;
				
				this.removeChild(g.content);
			}
		}
		
		override protected function releaseResources():void {
			this.removeAllComponents();
		}
	}

}